package com.hongtech.modules.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongtech.modules.job.entity.SysJobLog;
import com.hongtech.modules.job.mapper.SysJobLogMapper;
import com.hongtech.modules.job.service.SysJobLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements SysJobLogService {

    @Autowired
    private SysJobLogMapper sysJobLogMapper;

    @Override
    public SysJobLog getSysJobLogById(Long jobLogId) {
        return sysJobLogMapper.selectById(jobLogId);
    }

    @Override
    public void addJobLog(SysJobLog jobLog) {
        sysJobLogMapper.insert(jobLog);
    }

    @Override
    public List<SysJobLog> listSysLogJobs(SysJobLog sysJobLog, String[] dateRanges) {
        QueryWrapper<SysJobLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysJobLog.getJobName())) {
            queryWrapper.like("job_name", sysJobLog.getJobName());
        }
        if (sysJobLog.getStatus() != null) {
            queryWrapper.eq("status", sysJobLog.getStatus());
        }
        if (dateRanges.length > 0) {
            queryWrapper.between("create_time", dateRanges[0].trim(), dateRanges[1].trim());
        }
        queryWrapper.orderByDesc("create_time");
        return sysJobLogMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteJobLogByIds(List<String> ids) {
        QueryWrapper<SysJobLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("job_log_id", ids);
        sysJobLogMapper.delete(queryWrapper);
    }

}
